import Taro from "@tarojs/taro";
import {View, Image} from "@tarojs/components";
import AssetImage from "@/common/AssetImage";
import {Button} from '@antmjs/vantui'
import {useCallback} from "react";
import Utils from "@/utils/common";
import RouthPath from "@/common/RouthPath";
import LocalUtils from "@/utils/local_utils";
import {loginApi} from "@/common/apis/home_datasource";
import LogUtils from "@/utils/log";


export default function Login() {
  const loginWx = useCallback(() => {
    Taro.login().then(async (res) => {
      LogUtils.info(res.code)
      const res2 = await loginApi({code: res.code})
      if (res2) {
        LocalUtils.setToken(res2.token);
        LocalUtils.setUserinfo(res2.username, res2.avatar, res2.mobile,res2.authentication,res2.realname);
        Utils.switchTab({
          url: RouthPath.tabbarHome
        })
      }
    })
  }, []);

  return (
    <View style={{
      display: 'flex',
      alignItems: 'center',
      height: '100VH',
      justifyContent: 'center',
      flexDirection: 'column',
    }}
    >
      <View style={{
        display: 'flex',
        width: '80%',
        alignItems: 'center',
        flexDirection: 'column',
      }}
      >
        <Image
          style={{width: '100PX', height: '100PX'}}
          src={AssetImage.logo as string}
        />
        <Button icon='wechat' type='primary' block
                style={{width: '80%', marginBottom: '20PX', marginTop: '50PX'}}
                onClick={loginWx}
        >
          微信用户一键登录
        </Button>
        {/*<View style={{*/}
        {/*  display: 'flex',*/}
        {/*  alignItems: 'center',*/}
        {/*  justifyContent: 'center',*/}
        {/*  flexDirection: 'row',*/}
        {/*  color: 'green',*/}
        {/*}}*/}
        {/*      onClick={() => {*/}

        {/*        const url = Utils.getUrlWithObject(RouthPath.loginPhonePage, {*/}
        {/*          isOk: true*/}
        {/*        });*/}
        {/*        Utils.navigateTo({*/}
        {/*          url: url,*/}
        {/*        })*/}
        {/*      }}*/}
        {/*>*/}
        {/*  <Icon name='phone'></Icon>*/}
        {/*  <Text style={{marginLeft: '5PX'}}>账号密码登录</Text>*/}
        {/*</View>*/}
      </View>
    </View>
  )
}


